package com.zzh.ideas.core.common.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * description： <br>
 * createTime: 2018/11/2516:14 <br>
 *
 * @author zzh
 */
public class FileUtils {

    public static String getFileSuffix (String fileName) {
        int lastIndex = fileName.lastIndexOf(".");
        if(lastIndex < 0) {
            return "";
        }
        return fileName.substring(lastIndex, fileName.length());
    }

    public static boolean createDir(String path) {
        path = path.replaceAll("[\\\\/]*$", "");
        File file = new File(path);
        if (!file.exists()) {
            if (path.length() > 0 && path.indexOf("/") >= 0) {
                if (!createDir(path.substring(0, path.lastIndexOf("/")))) {
                    return false;
                }
            }
            if (!file.mkdir()) {
                return false;
            }
        }
        return true;
    }

    public static boolean createDir(File dir) {
        if(dir.exists()) {
            return true;
        }
        createDir(dir.getParentFile());
        return dir.mkdir();
    }


    public static boolean deleteFiles(String path) {
        path = path.replaceAll("[\\\\/]*$", "");
        File file = new File(path);
        if (file.exists()) {
            if (file.isDirectory()) {
                String[] fileNames = file.list();
                for (String fileName : fileNames) {
                    deleteFiles(path + "/" + fileName);
                }
            }
            file.delete();
        }
        return true;
    }

    /**
     * <p>方法描述: 按指定路径删除文件或目录</p>
     * <p>方法备注: </p>
     *
     * @param srcPath 要复制的文件或目录的全路径
     * @param desPath 要复制到的文件或目录的全路径
     * @return 返回创建的结果 true-成功 false-失败
     * <p>创建人：周志辉/p>
     * <p>创建时间：2016-11-11 下午5:02:21</p>
     */
    public static boolean copyFiles(String srcPath, String desPath) {
        srcPath = srcPath.replaceAll("[\\\\/]*$", "");
        desPath = desPath.replaceAll("[\\\\/]*$", "");
        File srcfile = new File(srcPath);
        File desfile = new File(desPath);
        if (srcfile.exists()) {
            if (srcfile.isDirectory()) {
                desfile.mkdir();
                String[] fileNames = srcfile.list();
                for (String fileName : fileNames) {
                    copyFiles(srcPath + "/" + fileName, desPath + "/" + fileName);
                }
            } else {
                copyFile(srcPath, desPath);
            }
        }
        return true;
    }

    /**
     * <p>方法描述: 按指定路径删除文件或目录</p>
     * <p>方法备注: </p>
     *
     * @param srcPath 要复制的文件或目录的全路径
     * @param desPath 要复制到的文件或目录的全路径
     * @return 返回创建的结果 true-成功 false-失败
     * <p>创建人：周志辉/p>
     * <p>创建时间：2016-11-11 下午5:02:21</p>
     */
    public static boolean copyFile(String srcPath, String desPath) {
        try (BufferedReader br = new BufferedReader(new FileReader(srcPath));
             BufferedWriter bw = new BufferedWriter(new FileWriter(desPath))) {
            String line = null;
            while ((line = br.readLine()) != null) {
                bw.write(line + "\n");
                bw.flush();
            }
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     *
     * Method Name：兼容本地windows测试环境，转换windows路径为可访问的url路径
     * Method Description: <br>
     *
     * @param filePath 文件路径
     * @return  转换后的路径
     * <p>creator：zhihui.zhou
     * <p>createTime：2018/12/15 15:59
     */
    public static String convertPath2Url(String filePath) {
        return System.getProperty("os.name").toLowerCase().indexOf("window") < 0
                ? filePath : (filePath.replaceAll("^[a-zA-Z]:", ""));
    }

    /**
     *
     * Method Name：兼容本地windows测试环境，转换windows路径为可访问的url路径
     * Method Description: <br>
     *
     * @param url 文件url
     * @return  转换后的文件路径
     * <p>creator：zhihui.zhou
     * <p>createTime：2018/12/15 15:59
     */
    public static String convertUrl2Path(String url) {
//        return System.getProperty("os.name").toLowerCase().indexOf("window") < 0
//                ? url : AppConstant.MediaPath.BASE.replaceAll("^([a-zA-Z]:).*$", "$1") + url;
        return url;
    }
}
